package com.ym.pms.config;

import com.ym.pms.resolvers.CurrentUserMethodArgumentResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * @Author: xybh
 * @Description:
 * @Date: Created in 16:13 2020/12/24
 * @Modified:
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Bean
    public CurrentUserMethodArgumentResolver getResolver(){
        return new CurrentUserMethodArgumentResolver();
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(getResolver());
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {

        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowCredentials(true)
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE","OPTIONS")
                .maxAge(3600);

    }
//    @Override
//    public void addCorsMappings(CorsRegistry registry) {
//        //添加全局的 CORS 配置
//        registry.addMapping("/**")  //匹配所有的 URL ,相当于全局配置
//                .allowedOrigins("/**") //允许所有请求来源
//                .allowCredentials(true) //允许Cookie
//                .allowedMethods("*") //允许所有请求 Method
//                .allowedHeaders("*") //允许所有请求 Header
//                .maxAge(1800L); //有效期1800秒，两小时
//    }
}
